package com.jdy.haoduoaiteacher.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jdy.haoduoaiteacher.enums.SelectionMode;
import com.jdy.haoduoaiteacher.model.StudentDeviceModel;
import com.jdy.haoduoaiteacher.util.Tools;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.ycsj.goldmedalnewconcept.R;

import java.util.ArrayList;

//  my love  story list adapter
@SuppressLint("InflateParams")
public class MyLoveStoryAdapter extends BaseAdapter {
  private Activity mContext;
  private LayoutInflater mInflater;
  private ArrayList<StudentDeviceModel> mList;
  private DisplayImageOptions options;
  private boolean isEditor = false;
  // private boolean isChecked = false;

  public boolean isPublishPhoto() {
    return isPublishPhoto;
  }

  public void setPublishPhoto(boolean publishPhoto) {
    isPublishPhoto = publishPhoto;
  }

  private boolean isPublishPhoto = false;


  private int highLightPosition = -1; // 正在播放

  public int getHighLightPosition() {
    return highLightPosition;
  }

  public void setHighLightPosition(int highLightPosition) {
    this.highLightPosition = highLightPosition;
    notifyDataSetChanged();
  }

  public SelectionMode SelectMode = SelectionMode.NONE;

  @SuppressWarnings("static-access")
  public void setSelectMode(SelectionMode sm) {
    SelectMode = sm;

    if (mList != null && !mList.isEmpty()) {
      if (SelectMode == SelectMode.SELECT_ALL) {
        for (StudentDeviceModel item : mList) {
          item.isChecked = true;
        }
      } else if (SelectMode == SelectMode.DESELECT_ALL) {
        for (StudentDeviceModel item : mList) {
          item.isChecked = false;
        }
      }
    }
  }

  public void remove(StudentDeviceModel object) {
    mList.remove(object);
    notifyDataSetChanged();
  }

  public ArrayList<StudentDeviceModel> getList() {
    return mList;
  }

  public void removeSelection() {
    for (StudentDeviceModel item : mList) {
      item.isChecked = false;
    }

    notifyDataSetChanged();
  }

  public void setEditor(boolean iseditor) {
    this.isEditor = iseditor;
  }

  // public void setChecked(boolean checked) {
  // this.isChecked = checked;
  // isEditor = true;
  // }

  public MyLoveStoryAdapter(Activity context, ArrayList<StudentDeviceModel> list) {
    this.mContext = context;
    this.mList = list;
    mInflater = LayoutInflater.from(mContext);
    // 用 displayImage 加载图片 的 配置参数
    options = new DisplayImageOptions.Builder()
        .showImageOnFail(R.drawable.default_img_listitem) // 下载 失败 显示的 图片
        .cacheInMemory(true) // 缓存
        .bitmapConfig(Bitmap.Config.RGB_565) // 图片格式
        .build();
    // 创建默认的ImageLoader配置参数
    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(
        mContext).writeDebugLogs() // 打印 log 信息
        .build();
    ImageLoader.getInstance().init(configuration);
  }

  @Override
  public int getCount() {
    return mList.size();
  }

  @Override
  public Object getItem(int arg0) {
    return mList.get(arg0);
  }

  @Override
  public long getItemId(int arg0) {
    return arg0;
  }

  public  View getSelectView () {
return mSelView;
  }

  private View mSelView;

  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
    StudentDeviceModel item = mList.get(position);
    final ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = mInflater.inflate(R.layout.mylovestory_teacher, null);
      holder.cover = (ImageView) convertView.findViewById(R.id.cover);
      holder.tv_deviceid = (TextView) convertView.findViewById(R.id.tv_deviceid);
      holder.tv_devicename = (TextView) convertView.findViewById(R.id.tv_devicename);
      holder.radiobtn = (CheckBox) convertView.findViewById(R.id.radiobtn);
      holder.rela_parent = (LinearLayout) convertView
          .findViewById(R.id.rela_parent);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }
    holder.cover.setScaleType(ImageView.ScaleType.FIT_XY);

    if (isPublishPhoto) {
      if (highLightPosition >=0 && highLightPosition == position ) {
        holder.rela_parent.setBackgroundColor(0xfffcf5da);
        mSelView = holder.rela_parent;
      } else {
        holder.rela_parent.setBackgroundColor(0xffffffff);
      }
    }

    if (Tools.isNotEmpty(item) && Tools.isNotEmpty(item.machine) && Tools.isNotEmpty(item.machine.avatar)) {
      ImageLoader.getInstance().displayImage(item.machine.avatar,
          holder.cover, options);
    }

      holder.tv_devicename.setText(item.nick_name);
      holder.tv_deviceid.setText("设备号: " + item.machine_id);

    if (isEditor) { // 处于编辑形态
      holder.radiobtn.setVisibility(View.VISIBLE);
    } else {
      holder.radiobtn.setVisibility(View.GONE);
    }

    if (isEditor) {
      holder.rela_parent.setClickable(true);
      holder.rela_parent.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
          holder.radiobtn.setChecked(!holder.radiobtn.isChecked());
          mList.get(position).isChecked = holder.radiobtn.isChecked();
        }
      });
    } else {
      holder.rela_parent.setClickable(false);
    }

    holder.radiobtn.setChecked(mList.get(position).isChecked);

//     if (isChecked) {
//     holder.radiobtn.setChecked(true);
//     } else {
//     holder.radiobtn.setChecked(false);
//     }
    return convertView;
  }

  private static class ViewHolder {
    private ImageView cover;
    private TextView tv_devicename;
    private TextView tv_deviceid;
    private CheckBox radiobtn;
    private LinearLayout rela_parent;
  }

}
